home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
telecomm
/
sticpsrc.lzh
/
SOURCE.ARC
/
FTPSERV.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-05-02
|
21KB
|
844 lines
/* FTP Server state machine - see RFC 959 */
#define LINELEN 128 /* Length of command buffer */
#include <stdio.h>
#include <ctype.h>
#include "global.h"
#include "mbuf.h"
#include "netuser.h"
#include "timer.h"
#include "tcp.h"
#include "ftp.h"
#ifdef ATARI_ST
# ifdef __TURBOC__
# include <tos.h>
# else
# include <osbind.h>
# endif
#endif
/* Command table */
static char *commands[] = {
"user",
#define USER_CMD 0
"acct",
#define ACCT_CMD 1
"pass",
#define PASS_CMD 2
"type",
#define TYPE_CMD 3
"list",
#define LIST_CMD 4
"cwd",
#define CWD_CMD 5
"dele",
#define DELE_CMD 6
"name",
#define NAME_CMD 7
"quit",
#define QUIT_CMD 8
"retr",
#define RETR_CMD 9
"stor",
#define STOR_CMD 10
"port",
#define PORT_CMD 11
"nlst",
#define NLST_CMD 12
"pwd",
#define PWD_CMD 13
"xpwd", /* For compatibility with 4.2BSD */
#define XPWD_CMD 14
"mkd ",
#define MKD_CMD 15
"xmkd", /* For compatibility with 4.2BSD */
#define XMKD_CMD 16
"xrmd", /* For compatibility with 4.2BSD */
#define XRMD_CMD 17
"rmd ",
#define RMD_CMD 18
"stru",
#define STRU_CMD 19
"mode",
#define MODE_CMD 20
#if (defined(ATARI_ST))
"xatr", /* own extension: set attribs */
#define XATR_CMD 21
#endif
NULLCHAR
};
/* Response messages */
static char sending[] = "150 Opening data connection for %s \"%s\"\r\n";
#if (defined(ATARI_ST))
static char xatrok[] = "200 \"%s\" XATR ok\r\n";
#endif
static char okay[] = "200 Ok\r\n";
static char portok[] = "200 Port command okay\r\n";
static char typeok[] = "200 Type OK\r\n";
static char banner[] = "220 %s FTP version %s ready at %.24s\r\n";
static char bye[] = "221 Goodbye!\r\n";
static char rxok[] = "226 File received OK\r\n";
static char txok[] = "226 File sent OK\r\n";
static char logged[] = "230 Logged in\r\n";
static char deleok[] = "250 File deleted\r\n";
static char mkdok[] = "257 \"%s\" directory created\r\n";
static char pwdmsg[] = "257 \"%s\" is current directory\r\n";
static char givepass[] = "331 Enter PASS command\r\n";
static char noconn[] = "425 Data connection reset\r\n";
static char badcmd[] = "500 Unknown command\r\n";
static char unsupp[] = "500 Unsupported command or option\r\n";
static char badport[] = "501 Bad port syntax\r\n";
#if (defined(ATARI_ST))
static char badxatr[] = "501 Bad XATR syntax\r\n";
#endif
static char only8[] = "501 Only logical bytesize 8 supported\r\n";
static char badtype[] = "501 Unknown type \"%s\"\r\n";
static char unimp[] = "502 Command not yet implemented\r\n";
static char notlog[] = "530 Please log in with USER and PASS\r\n";
#if (defined(ATARI_ST))
static char xatrfail[] = "550 \"%s\" XATR failed\r\n";
#endif
static char cantopen[] = "550 Can't read file \"%s\"\r\n";
static char delefail[] = "550 Delete failed\r\n";
static char noperm[] = "550 Permission denied\r\n";
static char cantmake[] = "553 Can't create \"%s\"\r\n";
static char nodir[] = "553 Can't read directory \"%s\"\r\n";
static struct tcb *ftp_tcb;
/* Start up FTP service */
ftp1(argc,argv)
int argc;
char *argv[];
{
struct socket lsocket;
void ftpscr(),ftpscs();
char tos = 0;
lsocket.address = ip_addr;
if(argc < 2)
lsocket.port = FTP_PORT;
else {
if((lsocket.port = atoi(argv[1])) == 0)
lsocket.port = FTP_PORT;
tos = get_tos(argv[2]);
}
ftp_tcb = open_tcp(&lsocket,NULLSOCK,TCP_SERVER,0,ftpscr,NULLVFP,ftpscs,tos,(char *)NULL);
return 0;
}
ftp0()
{
if(ftp_tcb != NULLTCB)
close_tcp(ftp_tcb);
return 0;
}
/* FTP Server Control channel State change upcall handler */
static
void
ftpscs(tcb,old,new)
struct tcb *tcb;
char old,new;
{
extern char hostname[],version[];
struct ftp *ftp,*ftp_create();
void ftp_delete();
long t;
switch(new){
/* Setting QUICKSTART piggybacks the server's banner on the SYN/ACK segment;
* leaving it unset waits for the three-way handshake to complete before
* sending the banner. Piggybacking unfortunately breaks some old TCPs,
* so its use is not (yet) recommended.
*/
#ifdef QUICKSTART
case SYN_RECEIVED:
#else
case ESTABLISHED:
#endif
if((ftp = ftp_create(LINELEN)) == NULLFTP){
/* No space, kill connection */
close_tcp(tcb);
return;
}
ftp->control = tcb; /* Downward link */
tcb->user = (char *)ftp; /* Upward link */
/* Set default data port */
ftp->port.address = tcb->conn.remote.address;
ftp->port.port = FTPD_PORT;
log_tcp(tcb,"open FTP");
time(&t);
tprintf(ftp->control,banner,hostname,version,ctime(&t));
break;
case CLOSE_WAIT:
close_tcp(tcb);
break;
case CLOSED:
if(tcb != ftp_tcb)
log_tcp(tcb,"close FTP");
if((ftp = (struct ftp *)tcb->user) != NULLFTP)
ftp_delete(ftp);
/* Check if server is being shut down */
if(tcb == ftp_tcb)
ftp_tcb = NULLTCB;
del_tcp(tcb);
break;
}
}
/* FTP Server Control channel Receiver upcall handler */
static
void
ftpscr(tcb,cnt)
struct tcb *tcb;
int16 cnt;
{
register struct ftp *ftp;
char c;
struct mbuf *bp;
void ftpcommand();
if((ftp = (struct ftp *)tcb->user) == NULLFTP){
/* Unknown connection, just kill it */
close_tcp(tcb);
return;
}
for (;;){
switch(ftp->state){
case COMMAND_STATE:
/* Assemble an input line in the session buffer. Return if incomplete */
while(recv_tcp(tcb,&bp,1) == 1){
pullup(&bp,&c,1);
switch(c){
case '\r': /* Strip cr's */
continue;
case '\n': /* Complete line; process it */
ftp->buf[ftp->cnt] = '\0';
ftpcommand(ftp);
ftp->cnt = 0;
goto again;
default: /* Assemble line */
if(ftp->cnt != LINELEN-1)
ftp->buf[ftp->cnt++] = c;
break;
}
}
/* else no linefeed present yet to terminate command */
return;
case SENDING_STATE:
case RECEIVING_STATE:
/* Leave commands pending on receive queue until
* present command is done
*/
return;
}
again:;
}
}
/* FTP server data channel connection state change upcall handler */
void
ftpsds(tcb,old,new)
struct tcb *tcb;
char old,new;
{
register struct ftp *ftp;
if((ftp = (struct ftp *)tcb->user) == NULLFTP ||
!tcpval(ftp->control)){ /* control session existing? */
/* Unknown connection. Kill it */
del_tcp(tcb);
return;
}
if(new == CLOSED){
/* Clear only if another transfer hasn't already started */
if(ftp->data == tcb)
ftp->data = NULLTCB;
del_tcp(tcb);
if(tcb->reason != NORMAL){
/* Data connection was reset, complain about it */
tprintf(ftp->control,noconn);
/* And clean up */
if(ftp->fp != NULLFILE && ftp->fp != stdout)
fclose(ftp->fp);
ftp->fp = NULLFILE;
ftp->state = COMMAND_STATE;
/* Kick command parser if something is waiting */
if(ftp->control->rcvcnt != 0)
ftpscr(ftp->control,ftp->control->rcvcnt);
return;
}
}
if(ftp->state == SENDING_STATE && (old == FINWAIT1 || old == CLOSING)){
/* We've received an ack of our FIN while sending; we're done */
ftp->state = COMMAND_STATE;
tprintf(ftp->control,txok);
/* Kick command parser if something is waiting */
if(ftp->control->rcvcnt != 0)
ftpscr(ftp->control,ftp->control->rcvcnt);
return;
}
if(ftp->state == RECEIVING_STATE && new == CLOSE_WAIT){
/* FIN received on incoming file */
#ifdef CPM
if(ftp->type == ASCII_TYPE)
putc(CTLZ,ftp->fp);
#endif
close_tcp(tcb);
if(ftp->fp != stdout)
fclose(ftp->fp);
ftp->fp = NULLFILE;
ftp->state = COMMAND_STATE;
tprintf(ftp->control,rxok);
/* Kick command parser if something is waiting */
if(ftp->control->rcvcnt != 0)
ftpscr(ftp->control,ftp->control->rcvcnt);
return;
}
}
/* Parse and execute ftp commands */
static
void
ftpcommand(ftp)
register struct ftp *ftp;
{
void ftpdr(),ftpdt(),ftpsds();
char *cmd,*arg,*cp,**cmdp,*file;
char *pathname();
char *mode;
struct socket dport;
int i;
#ifndef CPM
FILE *dir();
#endif
cmd = ftp->buf;
if(ftp->cnt == 0){
/* Can't be a legal FTP command */
tprintf(ftp->control,badcmd);
return;
}
#ifdef UNIX
/* Translate first word to lower case